package top.fzqblog.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;
import java.util.Map;

import top.fzqblog.firstapp.R;

/**
 * Created by 抽离 on 2017/8/15 0015.
 */

public class PicTextAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;

    private List<Map<String, Object>> list;


    public PicTextAdapter(Context context, List<Map<String, Object>> list) {
        layoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(convertView == null){
            Log.i("tag", "新的view");
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.pictextlayout, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.introduce = (TextView) convertView.findViewById(R.id.introduce);
            convertView.setTag(viewHolder);
        }else{
            Log.i("tag", "view复用了");
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.imageView.setBackgroundResource((Integer) list.get(position).get("iv"));
        viewHolder.title.setText((String)list.get(position).get("title"));
        viewHolder.introduce.setText((String)list.get(position).get("introduce"));
        
        return convertView;
    }

    private class ViewHolder{
        private ImageView imageView;

        private TextView title;

        private TextView introduce;
    }
}
